package com.spring.nbrbt.tools.otherutil;

import java.util.ArrayList;
import java.util.List;
public class UpdateMap {
	private int id;
    private String table;
    private List<Field> fields = new ArrayList<Field>();
    private List<Field> where = new ArrayList<Field>();

    public UpdateMap(String table) {
        this.table = table;
    }

    public static UpdateMap N(String table, String whereColumn, String whereValue, Object... fields) {
        UpdateMap updateMap = new UpdateMap(table);
        int fieldLength = fields.length;
        if(fieldLength < 2) {
            return null;
        } else {
            int i = 0;

            for(int count = fieldLength / 2; i < count; ++i) {
                updateMap.addField(String.valueOf(fields[i * 2]), String.valueOf(fields[i * 2 + 1]));
            }

            updateMap.addWhere(whereColumn, whereValue);
            return updateMap;
        }
    }

    public static UpdateMap N(String table, int id, Object... fields) {
        UpdateMap updateMap = new UpdateMap(table);
        int fieldLength = fields.length;
        if(fieldLength < 2) {
            return null;
        } else {
            int i = 0;

            for(int count = fieldLength / 2; i < count; ++i) {
                updateMap.addField(String.valueOf(fields[i * 2]), String.valueOf(fields[i * 2 + 1]));
            }

            updateMap.addWhere("id", id);
            return updateMap;
        }
    }

    public static UpdateMap N(String table, Object[] fields, Object[] wheres) {
        UpdateMap updateMap = new UpdateMap(table);
        int fieldLength = fields.length;
        int whereLength = wheres.length;
        if(fieldLength >= 2 && whereLength >= 2) {
            int i = 0;

            int count;
            for(count = fieldLength / 2; i < count; ++i) {
                updateMap.addField(String.valueOf(fields[i * 2]), String.valueOf(fields[i * 2 + 1]));
            }

            i = 0;

            for(count = fieldLength / 2; i < count; ++i) {
                updateMap.addWhere(String.valueOf(fields[i * 2]), String.valueOf(fields[i * 2 + 1]));
            }

            return updateMap;
        } else {
            return null;
        }
    }

    public UpdateMap addField(String name, String value) {
        this.fields.add(new Field(name, value, (String)null));
        return this;
    }

    public UpdateMap addField(String name, int value) {
        this.fields.add(new Field(name, String.valueOf(value), (String)null));
        return this;
    }

    public UpdateMap addField(String name, float value) {
        this.fields.add(new Field(name, String.valueOf(value), (String)null));
        return this;
    }

    public UpdateMap addField(String name, String value, boolean column) {
        this.fields.add(new Field(name, value, "=", column));
        return this;
    }

    public UpdateMap addField(String name, String value, String operator, boolean column) {
        this.fields.add(new Field(name, value, operator, column));
        return this;
    }

    public UpdateMap addWhere(String name, String value) {
        this.where.add(new Field(name, value, "="));
        return this;
    }

    public UpdateMap addWhere(String name, int value) {
        this.where.add(new Field(name, String.valueOf(value), "="));
        return this;
    }

    public UpdateMap addWhere(String name, String value, String operator) {
        this.where.add(new Field(name, value, operator));
        return this;
    }

    public UpdateMap addWhere(String name, int value, String operator) {
        this.where.add(new Field(name, String.valueOf(value), operator));
        return this;
    }

    public UpdateMap addWhere(String name, String value, boolean column) {
        this.where.add(new Field(name, value, "=", column));
        return this;
    }

    public UpdateMap addWhere(String name, String value, String operator, boolean column) {
        this.where.add(new Field(name, value, operator, column));
        return this;
    }

    public String getTable() {
        return this.table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public List<Field> getFields() {
        return this.fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

    public List<Field> getWhere() {
        return this.where;
    }

    public void setWhere(List<Field> where) {
        this.where = where;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
